package com.ease.map.handler.feature.draw {
    import com.ease.map.Map;
    import com.ease.map.common.enum.MapActionEnum;
    import com.ease.map.events.DrawEvent;
    import com.ease.map.feature.Feature;
    import com.ease.map.geometry.PointGeometry;
    import com.ease.map.geometry.basetype.Location;
    import com.ease.map.geometry.basetype.Pixel;
    import com.ease.map.layer.FeatureLayer;
    import com.ease.map.style.IStyle;
    import flash.events.KeyboardEvent;
    import flash.events.MouseEvent;
    import flash.ui.Keyboard;
    import com.ease.map.handler.feature.draw.AbstractDrawHandler;

    /**
     * Handler to draw point.
     */
    [ExcludeClass]
    public class DrawPointHandler extends AbstractDrawHandler {

        /**
         * The layer in which we'll draw
         */
        private var _drawLayer:FeatureLayer = null;

        /**
         * Single ID for point
         */
        private var id:Number = 0;

        private var pointDown:PointGeometry;

        private var pointUp:PointGeometry;

        /**
         *
         * @param map 地图对象
         * @param active 当为true时，才可绘制线
         * */
        public function DrawPointHandler(map:Map, handlername:String) {
            super(map, handlername);
        }

        override protected function registerListeners():void {
            map.operateMode = MapActionEnum.MAP_DRAW;
            if (this.map) {
                this.map.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
                this.map.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
                this.map.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
                this.map.stage.addEventListener(KeyboardEvent.KEY_DOWN, keyboardDown);
            }
        }

        override protected function unregisterListeners():void {
            if (this.map) {
                this.map.removeEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
                this.map.removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
                this.map.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
                this.map.stage.removeEventListener(KeyboardEvent.KEY_DOWN, keyboardDown);
            }
            this.map.operateMode = MapActionEnum.MAP_PAN;
            _snapLayerIds = null;
            _snapLayers = null;
            drawLayer.graphics.clear();
        }

        protected function keyboardDown(event:KeyboardEvent):void {
            switch (event.keyCode) {
                case Keyboard.ESCAPE:
                    this.map.dispatchEvent(new DrawEvent(DrawEvent.DRAW_POINT_FEATURE_END, null));
                    break;
            }
        }

        private function mouseMoveHandler(event:MouseEvent):void {
            super.mouseMoveSnapHandler(event);
        }

        private function mouseDownHandler(event:MouseEvent):void {

            var pixel:Pixel = new Pixel(event.currentTarget.mouseX, event.currentTarget.mouseY);
            var lonlat:Location = this.map.getLocationFromLayerPx(pixel);

            pointDown = new PointGeometry(lonlat.lon, lonlat.lat);

        }

        private function mouseUpHandler(event:MouseEvent):void {
            var pixel:Pixel = new Pixel(event.currentTarget.mouseX, event.currentTarget.mouseY);
            var lonlat:Location = this.map.getLocationFromLayerPx(pixel);

            pointUp = new PointGeometry(lonlat.lon, lonlat.lat);
            if (!pointUp.equals(pointDown)) {
                //是拖动不画

            }
            else {
                drawPoint(event);
            }
        }

        /**
         * Create a point and draw it
         */
        protected function drawPoint(event:MouseEvent):void {
            /**
             * 判断是否有捕捉点
             * 如果有则坐标为捕捉点的坐标
             * 如果没有，为当前鼠标按下的位置
             *
             * */

            //We draw the point
            if (drawLayer != null) {
                trace("Drawing point");
                var style:IStyle = drawLayer.geoStyle;

                var pixel:Pixel = new Pixel(drawLayer.mouseX, drawLayer.mouseY);
                var lonlat:Location = this.map.getLocationFromLayerPx(pixel);
                var feature:Feature;
                var point:PointGeometry;
                if (snapReturn != null) {
                    point = new PointGeometry(snapReturn.snapPoint.x, snapReturn.snapPoint.y);
                    //回调函数，供外界获取捕获设施
                    if (this.snapClickCallback != null) {
//                        this.snapClickCallback(snapReturn.snapFeature, point);
						this.snapClickCallback(snapReturn.snapFeatures, point);
                    }

                    snapReturn = null;

                }
                else {
                    point = new PointGeometry(lonlat.lon, lonlat.lat);
                }


                feature = new Feature(point, null, style, null);
                feature.name = id.toString();
                id++;
                drawLayer.addFeature(feature);
				feature.executeDrawing();
                this.map.dispatchEvent(new DrawEvent(DrawEvent.DRAW_POINT_FEATURE_END, feature));
            }
        }

    }
}

